package com.rong.juc_test.juc_base.lock;

import java.util.concurrent.TimeUnit;

/**
 * 演示死锁
 */
public class DeadLock {

    static Object a = new Object();
    static Object b = new Object();


    public static void main(String[] args) {
        new Thread(() -> {
            synchronized(a){
                System.out.println(Thread.currentThread().getName() + " 持有资源a，尝试获取资源b");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(b){
                    System.out.println(Thread.currentThread().getName() + " 获取资源b");
                }
            }
        },"A").start();

        new Thread(() -> {
            synchronized(b){
                System.out.println(Thread.currentThread().getName() + " 持有资源b，尝试获取资源a");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(a){
                    System.out.println(Thread.currentThread().getName() + " 获取资源a");
                }
            }
        },"B").start();
    }
}
